package com.airsystem.pos.bengkel.service.strategy;

import java.util.LinkedList;
import java.util.List;

import org.hibernate.Session;

import com.airsystem.pos.bengkel.entity.DetailSparepart;
import com.airsystem.pos.bengkel.interfaces.IStrategy;
import com.airsystem.pos.bengkel.service.SparepartServices;
import com.airsystem.pos.bengkel.util.HibernateUtil;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class SparepartOneStrategy implements IStrategy<DetailSparepart> {
	private final SparepartServices sparepartServices = new SparepartServices();

	public Iterable<DetailSparepart> get(DetailSparepart data) {
		List<DetailSparepart> list = new LinkedList<DetailSparepart>();
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			StringBuffer formatter = new StringBuffer("%");
			formatter.append(data.getSearchKeyword());
			formatter.append("%");

			String temp = String.format(SparepartServices.getSelect(), data.getSearchColumn(), formatter.toString());

			List<?> query = (List<?>) session.createQuery(temp).list();
			list = sparepartServices.getParser().doParse(query);
		} finally {
			session.close();
		}

		return list;
	}
}